package com.zrc.tt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Run {
    public static boolean isWindows() {
        if (System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 执行命令
     *
     * @param cmd
     * @throws Exception
     */
    public static boolean exec(String cmd) {
        System.out.println("开始执行命令===>" + cmd);
        try {
            if (System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS")) {
                cmd = "cmd /c " + cmd;
            }
            Process process = Runtime.getRuntime().exec(cmd);
            consumeInputStream(process.getInputStream());
            consumeInputStream(process.getErrorStream());
            process.waitFor();
            if (process.exitValue() != 0) {
                throw new RuntimeException("执行命令错误===>" + cmd);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 消费inputstream，并返回
     */
    public static void consumeInputStream(InputStream is) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String s;
        while ((s = br.readLine()) != null) {
            System.out.println(s);
        }
    }
}

